/*-
 * Copyright (c) 2006, 2007 FTS Team
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 4. Neither the name of the FTS Team nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *      MemoryDocumentInfoStorage.cpp
 *
 * $FTS$
 */
#include <MemoryDocumentInfoStorage.hpp>
#include <stdio.h>
#include <DocumentInfo.hpp>
#include <Document.hpp>
namespace FTSS // FullTextSearchSystem
{

//
// Constructor
//
MemoryDocumentInfoStorage::MemoryDocumentInfoStorage()
{
	;;
}

//
// A Destructor
//
MemoryDocumentInfoStorage::~MemoryDocumentInfoStorage() throw()
{
	;;
}

//
// Serialize and dump object
//
INT_32 MemoryDocumentInfoStorage::Dump(DataDumper * pObject)
{
	if (pObject -> StartDump(C_MEM_DB_TYPE, (CHAR_P)C_MEM_DB_READABLE) != 0) { return -1; }

	if (pObject -> Dump(&(iDocNum), sizeof(Size_t)) != 0)            { return -1; }

	// Dump data
	StorageHash::iterator ithData = oStorageHash.begin();
	while (ithData != oStorageHash.end())
	{
		// Hash Key
		if (pObject -> Dump(&(ithData -> first), sizeof(DocumentId_t)) != 0)  { return -1; }
		// Size of DocumentEntry array
		if (pObject -> Dump(&(ithData -> second), sizeof(DocumentInfo)) != 0) { return -1; }
		ithData++;
	}
	DocumentId_t iDummy = (DocumentId_t)-1;
	if (pObject -> Dump(&iDummy, sizeof(DocumentId_t)) != 0) { return -1; }
	if (pObject -> EndDump() != 0) { return -1; }

return 0;
}

//
// Restore and de-serialize object
//
INT_32 MemoryDocumentInfoStorage::Restore(DataDumper * pObject)
{
	if (pObject -> StartRestore(C_MEM_DB_TYPE, (CHAR_P)C_MEM_DB_READABLE) != 0) { return -1; }

	// Get number of documents
	if (pObject -> Restore(&iDocNum, sizeof(Size_t)) != 0)              { return -1; }

	oStorageHash.clear();

	DocumentId_t iDocId = 0;
	do
	{
		if (pObject -> Restore(&iDocId, sizeof(DocumentId_t)) != 0)          { return -1; }

		// End-of-index
		if (iDocId == (TokenHash_t)(-1)) { break; }

		DocumentInfo oDocumentInfo;
		// Read data
		if (pObject -> Restore(&oDocumentInfo , sizeof(DocumentInfo)) != 0) { return -1; }

		oStorageHash.insert(StoragePair(iDocId, oDocumentInfo));

	} while(iDocId != (DocumentId_t)-1);

	if (pObject -> EndRestore() != 0) { return -1; }

return 0;
}

//
// Get document information
//
DocumentInfo * MemoryDocumentInfoStorage::GetInfo(const DocumentId_t & iDocumentId)
{
	StorageHash::iterator itmHash = oStorageHash.find(iDocumentId);

	if (itmHash != oStorageHash.end()) { return &(itmHash -> second); }
return NULL;
}

//
// Store document information
//
void MemoryDocumentInfoStorage::StoreInfo(const DocumentInfo & oDocumentInfo)
{
	StorageHash::iterator itmHash = oStorageHash.find(oDocumentInfo.doc_id);

	if (itmHash != oStorageHash.end()) { itmHash -> second = oDocumentInfo; }
	else { oStorageHash.insert(StoragePair(oDocumentInfo.doc_id, oDocumentInfo)); }
}


void MemoryDocumentInfoStorage::DeleteDocument(const DocumentId_t &iDocumentId){
	DocumentInfo*  pDocForDelete = GetInfo(iDocumentId);
	fprintf(stderr, "delting doc with id=%llu\n", iDocumentId);
	if (pDocForDelete != NULL ) {
		pDocForDelete->flags = DOCUMENT_IS_DELETED;
		StoreInfo(*pDocForDelete);
		fprintf(stderr, "deleted doc with id=%llu\n", iDocumentId);
	} else {
		fprintf(stderr, "not deleted doc with id=%llu\n", iDocumentId);
	}
 	return;
}

MemoryDocumentInfoStorage MemoryDocumentInfoStorage::compressInfoStorage(FileDocumentStorage &DocumentStorage, FileDocumentStorage &DocumentStorage_compressed){
	fprintf(stderr, "COMPRESSINFOSTORAGE\n\n");
	StorageHash::iterator itmHash = oStorageHash.begin();
	while (itmHash != oStorageHash.end()){
		Document* d = DocumentStorage.GetDocument(itmHash->second.doc_id - 1);
		if(itmHash -> second.flags == DOCUMENT_IS_DELETED){ 
			fprintf(stderr, "information about document with id =%lld deleted \n\n", itmHash -> second.doc_id);
			oStorageHash.erase(itmHash->first);
		}else{
			DocumentId_t tmp_doc_id = DocumentStorage_compressed.StoreDocument(d);
			fprintf(stderr, "information about document with id =%lld NOT deleted. new_id=%lld", itmHash -> second.doc_id,tmp_doc_id);
			//Document * debug_doc = DocumentStorage_compressed.GetDocument(tmp_doc_id);
		//	fprintf(stderr, "information about document with id =%lld NOT deleted. new_id=%lld with text=%s\n\n", itmHash -> second.doc_id,tmp_doc_id, debug_doc->DocumentData());
		}
		itmHash++;
	}
	return *this;
}
} // namespace FTSS
// End.
